/**
 * related to map.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-20
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var rootview =ui("$");
var dingweiUtil = require("helper/bdmap");
var hashdata = mm("do_HashData");
var addressListView = ui("addressListView");
var listdata = mm("do_ListData");
addressListView.bindItems(listdata);
var search = ui("search");
var key = ui("key");
var mapBtn = ui("mapBtn");

//andriod返回键
page.on("back", function() {
    app.closePage();
}).on("loaded", function() {
	page.hideKeyboard();
});
//左上角-返回
var headerLeft = ui("headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});

var param = {};
var city = "";
var map = ui("mapView");
var dingwei = sm("do_BaiduLocation");
if (page.getData()) {
	var data = page.getData();
	map.setCenter({latitude:data.y, longitude:data.x});
	var mark1 = [{"id":"id1","latitude":data.y,"longitude":data.x,"url":"source://image/mark.png","info":data.address}];
	map.addMarkers({data:mark1});
	param.latitude = data.y;
	param.longitude = data.x;
	param.address = data.address
	if (data.cityName) {
		city = data.cityName;
		param.cityName = city;
	} else {
		dingwei.reverseGeoCode(data.y, data.x, function(data1, e1) {
			city = data1.city;
			param.cityName = city;
    	});
	}
	var address = [{"latitude":param.latitude,"longitude":param.longitude,"address":param.address,"name":"","select":1}];
	listdata.addData(address);
	addressListView.refreshItems();
} else {
	param = dingweiUtil.getDingwei();
	city = param.cityName;
	map.setCenter({latitude:param.latitude, longitude:param.longitude});
	var mark1 = [{"id":"id1","latitude":param.latitude,"longitude":param.longitude,"url":"source://image/mark.png","info":param.address}];
	map.addMarkers({data:mark1});
	var address = [{"latitude":param.latitude,"longitude":param.longitude,"address":param.address,"name":"","select":1,"current":1}];
	listdata.addData(address);
	addressListView.refreshItems();
}

map.on("touchMap",function(data, e){
	deviceone.print("-----点击地图-----"+JSON.stringify(data));
	mapBtn.enabled = false;
	dingwei.reverseGeoCode(data.latitude, data.longitude, function(data1, e1) {
		mapBtn.enabled = true;
		deviceone.print("-----点击地图-----"+JSON.stringify(data1));
		map.removeMarker({ids:["id1"]});
		var mark1 = [{"id":"id1","latitude":data.latitude,"longitude":data.longitude,"url":"source://image/mark.png","info":data1.address}
		];
		map.addMarkers({data:mark1});
		var address = [{"latitude":data.latitude,"longitude":data.longitude,"address":data1.address,"name":"","select":1}];
		listdata.removeAll();
		listdata.addData(address);
		addressListView.refreshItems();
		param = data1;
		param.latitude = data.latitude;
		param.longitude = data.longitude;
		param.cityName = param.city;
	});
});

search.on("touch", function(){
	page.hideKeyboard();
	var searchkey = key.text.trim();
	if (searchkey) {
		//var temp = {location:param.latitude+','+param.longitude,radius:'2000'};
		var temp = {"city":city};
		map.poiSearch(0, searchkey, temp, 0, 15, function(data, e) {
			deviceone.print("----附近列表-开始----");
			deviceone.print("----附近列表-----"+JSON.stringify(data));
			if (data) {
				var address = [];
				for (var i = 0; i < data.length; i++) {
					var latitude = data[i].pt.split(",")[0];
					var longitude = data[i].pt.split(",")[1];
					address.push({"latitude":latitude,"longitude":longitude,"address":data[i].address,"name":data[i].name,"select":0});
				}
				listdata.removeAll();
				address.unshift({"latitude":param.latitude,"longitude":param.longitude,"address":param.address,"name":"","select":1});
				listdata.addData(address);
				addressListView.refreshItems();
			}
		});
	}
});

//点击
addressListView.on("touch", function(data){
	mapBtn.enabled = true;
	var data_temp = listdata.getRange(0);
	param = data_temp[data];
	map.setCenter({latitude:param.latitude, longitude:param.longitude});
	map.removeMarker({ids:["id1"]});
	var mark1 = [{"id":"id1","latitude":param.latitude,"longitude":param.longitude,"url":"source://image/mark.png","info":param.address}];
	map.addMarkers({data:mark1});
	for (var i = 0; i < listdata.getCount(); i++) {
        if (i == data) {
        	listdata.updateOne(i, {"latitude":data_temp[i].latitude,
        		"longitude":data_temp[i].longitude,
        		"address":data_temp[i].address,
        		"name":data_temp[i].name,"select":1});
        } else {
        	listdata.updateOne(i, {"latitude":data_temp[i].latitude,
        		"longitude":data_temp[i].longitude,
        		"address":data_temp[i].address,
        		"name":data_temp[i].name,"select":0});
        }
    }
	addressListView.refreshItems();
});

mapBtn.on("touch", function() {
	app.closePage(param);
})